//
//  PageViewAvatarCell.swift
//  
//
//  Created by Ralf on 6/12/15.
//
//

import UIKit

class PageViewAvatarCell: UITableViewCell
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let screen      = UIScreen.mainScreen().bounds

    let __avatar_image_view_tag = 10
    let __alias_label_tag       = 20
    let __index_of_sale_tag_tag = 21

    let __favorites_button_tag = 30

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initAvatarImageView()
        initAliasLabel()
        initIndexOfSaleTag()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func setData(data: NSMutableDictionary)
    {
        let article = data["article"] as! NSDictionary
        let contact = article["contact"] as! NSDictionary

        setAvatarImageView(contact["shopLogoURL"] as? String)
        setAliasLabel(contact["alias"] as! String)
        setIndexOfSaleTag(article["indexOfSaleTags"] as? String)
    }

    func setAvatarImageView(thumbnailURL: String?)
    {
        if thumbnailURL != nil
        {
            let imageView = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
            let url       = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
    }

    func setAliasLabel(alias: String)
    {
        let label = self.viewWithTag(__alias_label_tag) as! UILabel
        label.text = alias
    }

    func setIndexOfSaleTag(indexOfSaleTag: String?)
    {
        let label = self.viewWithTag(__index_of_sale_tag_tag) as! UILabel
        label.text = indexOfSaleTag
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y: 10.0, width: 40.0, height: 40.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tag
            imageView!.layer.cornerRadius = 20.0
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
    }

    func initAliasLabel()
    {
        var label = self.viewWithTag(__alias_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 60, y: 10, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __alias_label_tag
            self.addSubview(label!)
        }
    }

    func initIndexOfSaleTag()
    {
        var label = self.viewWithTag(__index_of_sale_tag_tag) as? UILabel
        if label == nil
        {
            let frame = CGRect(x: screen.width - 110, y: 10, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.textAlignment = .Right
            label!.tag = __index_of_sale_tag_tag
            self.addSubview(label!)
        }
    }
}
